package com.java.array;

public class BinarySearch {

	public static void main(String[] args){
		int[] i = {1,2,3,4,6,7,8,9,11};
		System.out.println(binary_search(i,0,8,7));
	}

	public static int binary_search(int arr[], int low, int high, int key)
	{
		int mid = low + (high - low) / 2; // Do not use (low + high) / 2 which might encounter overflow issue
		if (low > high)
			return -low - 1;
		else
		{
			if (arr[mid] == key)
				return mid;
			else if (arr[mid] > key)
				return binary_search(arr, low, mid-1, key);
			else
				return binary_search(arr, mid+1, high, key);
		}
	}

}
